﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using OGS.Contracts;
using OGS.Utility;
using OGS.Web.Models.Admin;

namespace OGS.Web.Controllers.Admin
{
    public partial class AdminController : AdminBaseController
    {
        /// <summary>
        /// Save UOM Action Result
        /// </summary>
        /// <returns></returns>
        [ActionName("Save-UOM")]
        public ActionResult SaveUOM(int? ID)
        {
            SaveUOMModel SaveUOMModel = new SaveUOMModel();
            SaveUOMModel.IsActive = true;
            if(ID.HasValue)
            {
                //Get UOM from DB
                SaveUOMModel.GetUOMDetails(ID.Value);

                SaveUOMModel.IsSuccess = Session["Success"] == null ? false : true;
                Session["Success"] = null;

            }
            return View("~/Views/Admin/UOM/SaveUOM.aspx", SaveUOMModel);
        }

        /// <summary>
        /// Save UOM in DB
        /// </summary>
        /// <param name="oSaveUOMModel"></param>
        /// <returns></returns>
        [HttpPost]
        [ValidateAntiForgeryToken]
        [ActionName("Save-UOM")]
        public ActionResult SaveUOM(SaveUOMModel SaveUOMModel)
        {
            if (ModelState.IsValid)
            {
                Result result = (Result)SaveUOMModel.SaveUOM();
                if (result == Result.Success)
                {
                    Session["Success"] = true;
                    return Redirect("/admin/save-uom/" + SaveUOMModel.ID.ToString());
                }
                else if (result == Result.Duplicate)
                {
                    ModelState.AddModelError("Name", "The name already exists.");
                }
                else
                {
                    ModelState.AddModelError("Name", "Some error occurred. Please try again later.");
                }
                
            }
            return View("~/Views/Admin/UOM/SaveUOM.aspx", SaveUOMModel);
        }

        /// <summary>
        /// UOM List Action
        /// </summary>
        /// <returns></returns>
        [ActionName("UOM-List")]
        public ActionResult UOMList()
        {
            SearchAttributes SearchAttributes = GetSearchAttributesFromQuesryString();

            UOMListModel UOMListModel = new UOMListModel();
            UOMListModel.Populate(SearchAttributes);
            UOMListModel.SearchAttributes = SearchAttributes;

            return View("~/Views/Admin/UOM/UOMList.aspx", UOMListModel);

        }
    }
}
